package com.tx.collections.queue;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 *
 */
public class ArrayBlockingQueueDemo {
    public static void main(String[] args) throws InterruptedException {
        //测试异常组
//        testEx();

        //测试阻塞组
//        testBlock();

        //测试超时组
        testTimeout();
    }

    private static void testTimeout() throws InterruptedException {
        ArrayBlockingQueue queue = new ArrayBlockingQueue(2);
        queue.offer(1,3, TimeUnit.SECONDS);
        queue.offer(2,3, TimeUnit.SECONDS);
        queue.offer(3,3, TimeUnit.SECONDS);
        queue.offer(4,3, TimeUnit.SECONDS);
        queue.poll(3,TimeUnit.SECONDS);
        queue.poll(3,TimeUnit.SECONDS);
        queue.poll(3,TimeUnit.SECONDS);
        queue.poll(3,TimeUnit.SECONDS);
        queue.poll(3,TimeUnit.SECONDS);
    }

    private static void testBlock() throws InterruptedException {
        ArrayBlockingQueue queue = new ArrayBlockingQueue(10);
        queue.put(1);
        queue.take();
    }

    private static void testEx() {
        ArrayBlockingQueue queue = new ArrayBlockingQueue(10);
        queue.add(1);
        queue.add(2);
        queue.add(3);
        queue.add(4);
        System.out.println(queue.remove());
        queue.remove(3);

        System.out.println(queue.element());
    }
}